package net.oschina.android.ui.fragment;

import net.oschina.android.R;
import net.oschina.android.adapter.discusstionAdapter;
import net.oschina.android.adapter.newAdapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;

/**
 * @author HuangWenwei
 *
 * @date 2014年9月1日
 */
public class DiscussionsFragment extends Fragment {
	
	private FragmentTabHost mTabHost;
	private ViewPager mViewpage;
	private TabContentFactory mFactory = new TabContentFactory() {
		
		@Override
		public View createTabContent(String tag) {
			View v = new View(getActivity());
			v.setMinimumHeight(0);
			return v;
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		mTabHost = new FragmentTabHost(getActivity());
		mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.newsrealtabcontent);
		mTabHost.addTab(mTabHost.newTabSpec("quesion").setIndicator("问答").setContent(mFactory), QustionFragment.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("share").setIndicator("分享").setContent(mFactory), ShareFragment.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("comprehensive").setIndicator("综合").setContent(mFactory), ComprehensiveFragment.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("career").setIndicator("职业").setContent(mFactory), CareerFragment.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("sitetast").setIndicator("站务").setContent(mFactory), SiteTastFragment.class, null);
		mTabHost.setOnTabChangedListener(mTabHost);
		
		View fragContent = inflater.inflate(R.layout.viewpage, container,false);
		mViewpage = (ViewPager) fragContent.findViewById(R.id.viewpager);
		mViewpage.setAdapter(new discusstionAdapter(getChildFragmentManager()));
		mViewpage.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				mTabHost.setCurrentTab(arg0);
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				if(tabId.equals("quesion"))
					mViewpage.setCurrentItem(0);
				else if(tabId.equals("share"))
					mViewpage.setCurrentItem(1);
				else if(tabId.equals("comprehensive"))
					mViewpage.setCurrentItem(2);
				else if(tabId.equals("career"))
					mViewpage.setCurrentItem(3);
				else if(tabId.equals("sitetast"))
					mViewpage.setCurrentItem(4);
			}
		});
		return fragContent;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		mTabHost = null;
	}
	
}
